home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlb20 / lib / mktemp.c < prev    next >
C/C++ Source or Header  |  1991-08-04  |  2KB  |  64 lines

  1. /* From the TOS GCC library by jrd */
  2. /* modified to accept only template with trailing XXX's (really should reqire
  3.  * that there be six trailing X's)
  4.  */
  5.  
  6. #include <stddef.h>
  7. #include <fcntl.h>
  8. #include <assert.h>
  9. #include <unistd.h>
  10. #include "lib.h"
  11.  
  12. #define TEN_MUL(X)    ((((X) << 2) + (X)) << 1)
  13.  
  14. extern int __mint;
  15.  
  16. char * mktemp(pattern)
  17. char * pattern;
  18. {
  19.   char * p, * q;
  20.   long tempnum, nx;
  21.   static int startat = 0;
  22.  
  23.   assert((pattern != NULL));
  24.  
  25.   /* scan back over X's */
  26.   for(p = pattern; *p; p++) ;
  27.   for(q = --p; *q == 'X'; --q) ;
  28.   if((nx = p - q) == 0)  /* # of X's */
  29.     return NULL;
  30.  
  31.   q++;
  32.  
  33.   /* if MiNT is active and there's room, put in the pid */
  34.   /* we need 5 X's for this: up to 3 for the pid, and up to 2 for the
  35.      extra number */
  36.   if (__mint && nx > 4 && startat < 256) {
  37.     (void) _itoa(getpid(), q, 10);
  38.     while (*q) q++;
  39.     (void) _itoa(startat++, q, 16);
  40.     return pattern;
  41.   }
  42.  
  43.   /* calc the #'s to try for X's, for 2 X's 10-99 and so on */
  44.   for(tempnum = 1; --nx > 0; tempnum = TEN_MUL(tempnum)) ; /* [lower */
  45.   nx = TEN_MUL(tempnum);                  /* upper) */
  46.  
  47.   tempnum += startat;  /* dont always start at [lower, start at lower+startat */
  48.   if(tempnum >=nx )
  49.   {
  50.       tempnum -= startat;
  51.       startat = 0;
  52.   }
  53.   else 
  54.       startat++;
  55.  
  56.   for(; tempnum < nx; tempnum++)
  57.   {
  58.     (void) _ltoa(tempnum, q, 10); /* assumption: strrev reverses in place */
  59.     if(access(pattern, F_OK))    /* using access takes care of unx2dos also */
  60.     return pattern;
  61.   }
  62.   return NULL;
  63. }
  64.